package com.stb.llh.controller;

import com.stb.llh.service.UserService;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 * @Author Lee
 * @Date 2021/11/1
 * @Description
 */

@Controller
public class IndexController {



//    @GetMapping("/index")
//    @ResponseBody
//    public String index(){
//        return "hello world";
//    }

    @GetMapping("/show")
    @ResponseBody
    public String show() throws IOException, InterruptedException {

        FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop102:9000"), new Configuration(), "atguigu");



        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isFile()) {
                System.out.println("是文件");
                System.out.println(fileStatus.getPath());
                System.out.println(fileStatus.getLen());
            } else {
                System.out.println("不是文件");
                System.out.println(fileStatus.getPath());
            }
        }
        return fileStatuses.toString();
    }

    @RequestMapping("/a")
    public String a(){
        return "index";
    }


    @RequestMapping("/index")
    public String index(HttpServletRequest req){
        if (req.getSession().getAttribute("user")!=null) {
            return "user/ai/index";
        }
        return "error/404";
    }

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/insertEs")
    public boolean insertEs(String key) throws IOException {

        return userService.insertData();

    }

    @RequestMapping("/es")
    public List getEs(String key) throws IOException {

        List<Map<String, Object>> maps = userService.searchPage(key);
        return maps;

    }


}
